package com.rongzhe.house.index;

import com.alibaba.fastjson.JSONObject;
import com.rongzhe.house.house.criteria.HouseMapCriteria;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.search.MultiSearchResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Chengjia
 * @date 2018/3/17 15:27
 */
@RestController
@RequestMapping("/lasticsearch")
public class TestController {

    @Autowired
    private Client client;

    @ApiOperation(value = "地图搜索", notes = "houseMapCriteria", httpMethod = "GET")
    @RequestMapping("/mapSearch")
    public JSONObject mapSearch(HouseMapCriteria houseMapCriteria){

        JSONObject result=new JSONObject();

        //创建builder
        BoolQueryBuilder builder = QueryBuilders.boolQuery();
        //设置多字段组合模糊搜索
        if(StringUtils.isNotBlank(houseMapCriteria.getQuery())){
            builder.must(QueryBuilders.multiMatchQuery(houseMapCriteria.getQuery(),"name"));
        }
        if(StringUtils.isNotBlank(houseMapCriteria.getLat()+"")&&StringUtils.isNotBlank(houseMapCriteria.getLon()+"")){
            GeoPoint geoPoint = new GeoPoint();
            geoPoint.reset(houseMapCriteria.getLat(),houseMapCriteria.getLon());
            builder.must(QueryBuilders.geoDistanceQuery("location")
                    .point(geoPoint)
                    .distance(1, DistanceUnit.KILOMETERS));
        }
        SearchRequestBuilder srb1 = client
                .prepareSearch("housesearch").setQuery(builder);
        MultiSearchResponse sr = client.prepareMultiSearch()
                .add(srb1)
                .get();
        List<Map<String,Object>> datas=new ArrayList<Map<String,Object>>();
        long total=0;
        for (MultiSearchResponse.Item item : sr.getResponses()) {

            SearchResponse response = item.getResponse();

            if (response == null) {
                continue;
            }

            SearchHit[] searchHits = response.getHits().getHits();
            total=response.getHits().getTotalHits();
            if (searchHits != null && searchHits.length != 0) {
                for (int i = 0; i < searchHits.length; i++) {
                    Map<String, Object> source = searchHits[i].getSourceAsMap();
                    datas.add(source);
                }
            }
        }
        long totalPage=total%houseMapCriteria.getPageSize()==0?(total/houseMapCriteria.getPageSize()):(total/houseMapCriteria.getPageSize()+1);
        result.put("totalPage",totalPage);
        result.put("total",total);
        result.put("data",datas);
        return result;
    }
}
